package com.mission.game.otherscreens;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.mission.game.R;
import com.mission.game.components.InviteAdapter;
import com.mission.game.creategame.CreateNewGameScreen;
import com.mission.game.playgame.PlayGameScreen;
import com.mission.game.utils.Game;
import com.mission.game.utils.User;

public class MainScreen extends Activity {

	protected static final int PLAY_GAME = 4;
	private static final int CREATE_GAME = 3;
	private User _loggedUser;
	private ArrayList<String> _invitations;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_screen);
		// Get user
		Bundle extras = getIntent().getExtras();
		_loggedUser = (User) extras.getSerializable("user");

		setLists();
	}

	private void setLists() {
		updateMissionsLists();
		updateInvitationsLists();
	}
	
	private void updateMissionsLists() {
		// Set game list
		ListView gameLv = (ListView) findViewById(R.id.game_list);
		// Get games according to user.
		final ArrayList<String> games = _loggedUser.getGameNames();
		ArrayAdapter<String> gameAdapter = new ArrayAdapter<String>(this,
				R.layout.game_item, games);
		gameLv.setAdapter(gameAdapter);
		gameLv.setTextFilterEnabled(true);
		gameLv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Game clickedGame = _loggedUser.getGame(games.get(position));
				Intent i = new Intent(getApplicationContext(),
						PlayGameScreen.class);
				i.putExtra("game", clickedGame);
				startActivityForResult(i, PLAY_GAME);
			}
		});
	}
	
	private void updateInvitationsLists() {
		// Set invite list.
		ListView inviteLv = (ListView) findViewById(R.id.invitation_list);
		_invitations = _loggedUser.getInviteGameNames();
		// Scrolling check not good... it makes the "create game" button
		// disappear.
		InviteAdapter inviteAdapter = new InviteAdapter(this,
				R.layout.game_invite_item, _invitations);
		inviteLv.setAdapter(inviteAdapter);
		inviteLv.setTextFilterEnabled(true);
		inviteLv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplicationContext(),
						"Click ListItem Number " + position, Toast.LENGTH_LONG)
						.show();
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		setLists();
	}

	
	@Override
	public void onBackPressed() {
		// super.onBackPressed();
	}

	public void onCreateNewGameClicked(View v) {
		Intent i = new Intent(this, CreateNewGameScreen.class);
		startActivityForResult(i, CREATE_GAME);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if (data == null || data.getExtras() == null || !data.getExtras().containsKey("game"))
			return;
		Game game = (Game) data.getExtras().get("game");
		
		switch (requestCode) {
		case CREATE_GAME:
			if (resultCode == RESULT_OK) {
				_loggedUser.addNewGame(game);
			}
			break;
		case PLAY_GAME:
			if (resultCode == RESULT_OK) {
				_loggedUser.updateGame(game);
			} else if (resultCode == RESULT_FIRST_USER) {
				_loggedUser.removeGame(game.getName());
			} 			
			break;

		default:
			break;
		}
	}

	public void removeMissionItem(final int position) {
		new AlertDialog.Builder(this)
				.setIcon(android.R.drawable.ic_dialog_alert)
				.setTitle("Remove Item")
				.setMessage(
						"Are you sure you want to remove Invitation?")
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								_loggedUser.removeGame(_invitations.get(position));
								updateInvitationsLists();
							}

						}).setNegativeButton("No", null).show();
	}

	public void addMissionItem(int position) {
		_loggedUser.acceptInvitation(_invitations.get(position));
		updateMissionsLists();
		updateInvitationsLists();
	}

	public void showMissionData(final int position) {
		new AlertDialog.Builder(this)
		.setTitle("Mission Description:")
		.setMessage(_loggedUser.getGames().get(_invitations.get(position)).getDescription())
		.setPositiveButton("Accept",
				new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					addMissionItem(position);
				}
			})
		.setNeutralButton("Cancel", null).show();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.about:
			startActivity(new Intent(this, About.class));
			return true;
		case R.id.logout:
			getIntent().putExtra("user", _loggedUser);
			setResult(RESULT_OK, getIntent());
			finish();
			return true;
		}
		return false;
	}
}